<?php
namespace app\payclass\model;

use app\BaseModel;
use think\facade\Db;

class Auth extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_payclass_auth';


    /**
     * type 0 订单
     *
     * 1 活动
     *
     * 2免费
     *
     * 3 会员免费领取
     *
     * 规则:只要领取后就永久有观看权利
     */


    /**
     * @author chenniang
     * @DataTime: 2020-12-08 10:51
     * @功能说明:列表
     */
    public function cateList($dis,$page){

        $data = $this->where($dis)->order('top')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:详情
     */
    public function authInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)? $data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function authAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function cateUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-16 11:34
     * @功能说明:我的课程列表
     */
    public function classList($user_id,$page=10){

        $dis[] = ['a.user_id','=',$user_id];

        $data = $this->alias('a')
                ->join('longbing_card_payclass_list b','a.class_id = b.id')
                ->where($dis)
                ->field('a.id,a.class_id,b.title,b.cover,b.status')
                ->group('b.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            $record_model = new LookRecord();

            $video_model  = new Video();

            foreach ($data['data'] as $k=>$v){

                $map = [

                    'class_id'=> $v['class_id'],

                    'status'  => 1
                ];
                //视频总条数
                $data['data'][$k]['all_count']  = $video_model->where($map)->count();
                //观看记录
                $data['data'][$k]['look_count'] = $record_model->lookCount($user_id,$v['class_id']);
                //当前观看的视频
                $data['data'][$k]['now_video']  = $record_model->nowVideo($v['class_id'],$user_id);

            }

        }

        return $data;

    }














}